package com.android.pinprovince.activity;

import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.pinprovince.R;
import com.android.pinprovince.app.BaseActivity;
import com.android.pinprovince.model.group.MainList;
import com.android.pinprovince.model.group.MainListCallback;
import com.android.pinprovince.utils.ConstantsUrl;
import com.android.pinprovince.utils.HttpUtils;
import com.android.pinprovince.utils.MyLog;
import com.android.pinprovince.utils.ObjectUtils;
import com.android.pinprovince.view.recycleview.CustomLoadMoreView;
import com.android.pinprovince.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class MainNavActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.click_resetnetwork_msg)
    TextView clickResetnetworkMsg;
    @BindView(R.id.click_resetnetwork_refresh)
    TextView clickResetnetworkRefresh;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    private boolean networkConnected;
    private NavlistAdapt adapter;
    private int page = 1;
    private MainNavActivity mcontext;
    private String id;
    private String sorttitle;
    private GridLayoutManager mGridLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_recycle_refresh);
        ButterKnife.bind(this);
        mcontext = MainNavActivity.this;
        sorttitle = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_TITLE);
        id = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_SORT_SHOW_LINK);
        initTitle(0, sorttitle);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        initRecycle();
        Refresh();
        if (networkConnected) {
            GetListData(page);
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    private void initRecycle() {
        adapter = new NavlistAdapt(mcontext, null);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        mGridLayoutManager = new GridLayoutManager(mcontext, 2);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.GetToTop((ImageView) findViewById(R.id.top), 0);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                ObjectUtils.ToDetailActivity(mcontext, 1, adapter.getItem(position).getId(), "");
            }
        });
    }

    private void Refresh() {
        mRefreshLayout.setColorSchemeResources(R.color.app_title);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                if (networkConnected) {
                    page = 1;
                    GetListData(page);
                } else {
                    ObjectUtils.toast(mcontext, "网络连接出现异常");
                    mRefreshLayout.setRefreshing(false);
                }
            }
        });
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        page = 1;
        GetListData(page);
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        GetListData(page);
    }

    //获取主页列表数据
    private void GetListData(final int page) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MAINTAB_NAV_LIST + "?page=" + page + "&indexcat=" + id;
        MyLog.e("地址", address);
        if (networkConnected) {
            HttpUtils.get(address, null, mCallback);
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(mcontext, "网络连接出现异常");
            adapter.loadMoreFail();
        }
    }

    MainListCallback mCallback = new MainListCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            mRefreshLayout.setRefreshing(false);
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
        }

        @Override
        public void onResponse(MainList response, int id) {
            mRefreshLayout.setRefreshing(false);
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
            if (response.getCode() == 0) {
                if (response.getData() == null || response.getData().getList() == null || response.getData().getList().size() == 0) {
                    if (page == 1) {
                        noData.setVisibility(View.VISIBLE);
                    } else {
                        adapter.loadMoreEnd();
                    }
                    return;
                }
                List<MainList.MainListdata.Mainlistdata> list = response.getData().getList();
                if (list != null && list.size() > 0) {
                    if (page == 1) {
                        adapter.setNewData(list);
                    } else {
                        adapter.addData(list);
                        adapter.loadMoreComplete();
                    }
                } else {
                    adapter.loadMoreEnd();
                }
            } else {
                noData.setVisibility(View.VISIBLE);
                noDataTv.setVisibility(View.VISIBLE);
                noDataTv.setText(response.getMsg());
            }
        }
    };

    class NavlistAdapt extends BaseQuickAdapter<MainList.MainListdata.Mainlistdata, BaseViewHolder> {

        public NavlistAdapt(Context context, List<MainList.MainListdata.Mainlistdata> data) {
            super(R.layout.item_grid_detail_like, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, MainList.MainListdata.Mainlistdata mainlistdata) {
            ((TextView) holder.getView(R.id.item_grid_detail_like_number_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.setText(R.id.item_grid_detail_like_title, mainlistdata.getName());
            holder.setText(R.id.item_grid_detail_like_number_after, "￥" + mainlistdata.getTeam_price());
            holder.setText(R.id.item_grid_detail_like_number_before, "￥" + mainlistdata.getPrice());
            holder.setText(R.id.item_grid_detail_like_tuanshu, "已团" + mainlistdata.getSales_nums() + "件");
            ObjectUtils.photo2(mContext, mainlistdata.getImg_src(), ((ImageView) holder.getView(R.id.item_grid_detail_like_img)));
        }
    }
}
